<?php
session_start();
class editarQuestao extends Tpage{
	public $_vetor  = null;
	public $_questao = null;



	public function saveRecord($sender, $param){
		
		
		$questao = questaoRecord::finder()->findbyPk($_SESSION['idQuestao']);
				
		$tipoQuestao = $idDisciplina = $this->collectSelectionResult($this->Tipo);
		if($tipoQuestao == 1){
			$questao->BooleanIsDescritiva = false;
		}else{
			$questao->BooleanIsDescritiva = true;
		}
		
		if($this->collectSelectionResult($this->Tema) == ""){
			$questao->FKCodLongTema = $this->Tema->PromptValue;	
		}else{
			$questao->FKCodLongTema = $this->collectSelectionResult($this->Tema);	
		}
		
		$questao->FKCodLongUsuario = $_SESSION['idUsuario'];
		
		if($this->collectSelectionResult($this->Disciplina) == ""){
			$questao->FKCodStrDisciplina = $this->Disciplina->PromptValue;
		}else{
			$questao->FKCodStrDisciplina = $this->collectSelectionResult($this->Disciplina);	
		}
		
		$questao->IntDificuldade = $this->collectSelectionResult($this->Dificuldade);
		$questao->IntTemp = $this->collectSelectionResult($this->Tempo);
		$questao->StrHtml = $this->Content->SafeText;

		
		$questao->save();
		$_SESSION['isEdit'] = true;
		$this->Response->  redirect("?page=questao/buscarQuestao");
	}

	public function onLoad($param) {
		parent::onLoad($param);
		if(!$this->IsPostBack)
		{
			
			$_questao = questaoRecord::finder()->findbyPk($_SESSION['idQuestao']);
			$_disciplina = disciplinaRecord::finder()->findAll();

			$_SESSION['disciplina'] = $_disciplina;

			foreach ($_disciplina as $dados=>$valor){

				if($valor->PKCodStrDisciplina != $_questao->FKCodStrDisciplina){
					$vetor []= (array(
            					'PKCodStrDisciplina'=>$valor->PKCodStrDisciplina,
            					'disciplina'=>$valor->StrDisciplina, 

					));
				}

			}
			$data = $vetor;

			$this->Disciplina->DataSource=$data;
			$this->Disciplina->dataBind();

				
			/*
			 * Busca o tema que esta vinculado com a disciplina, conforme cadastrado no banco de dados,
			 * Apos a busca popula o componenete de seleção com o(s) tema(s).
			 */
			$_tema = temaRecord::finder()->findAll('FKCodStrDisciplina = ?',$_questao->FKCodStrDisciplina);
				
				
			foreach ($_tema as $dados=>$valor1){
				if($valor1->PKCodLongTema != $_questao->FKCodLongTema){
					$vetor1 []= (array(
            					'PKCodLongTema'=>$valor1->PKCodLongTema,
            					'tema'=>$valor1->StrTema,
					));
				}

			}
			$data1 = null;
			$data1 = $vetor1;

			$this->Tema->DataSource=$data1;
			$this->Tema->dataBind();
			
			
			$this->Dificuldade->DataSource=$data1;
			$this->Dificuldade->dataBind();
			

			$this->Content->Text = $_questao->StrHtml;

		}else{
			//Busca o tema vinculado com a disciplina selecionada
			$this->buscaTemaByDisciplina();

		}
	}

	public function buscaTemaByDisciplina(){

		$idDisciplina = $this->collectSelectionResult($this->Disciplina);

		if($idDisciplina == ""){
			$_tema = temaRecord::finder()->findAll('FKCodStrDisciplina = ?',$this->Disciplina->PromptValue);
		}else{
			$_tema = temaRecord::finder()->findAll('FKCodStrDisciplina = ?',$idDisciplina);
		}

		foreach ($_tema as $dados=>$valor1){

			$vetor1 []= (array(
            					'PKCodLongTema'=>$valor1->PKCodLongTema,
            					'tema'=>$valor1->StrTema,
			));
		}
		$data1 = $vetor1;

		$this->Tema->DataSource=$data1;
		$this->Tema->dataBind();
	}

	/*
	 * Busca os valores selecionados nos campos de seleçao
	 * o parametro de entrada o o id do campo de selecao.
	 */
	protected function collectSelectionResult($input)
	{
		$indices=$input->SelectedIndices;
		$result='';
		foreach($indices as $index)
		{
			$item=$input->Items[$index];
			$result = $item->Value;
				
		}

		return $result;
	}

	public function getStrDisciplina(){
		$_questao = questaoRecord::finder()->findbyPk($_SESSION['idQuestao']);
		$_disciplina = disciplinaRecord::finder()->findbyPk($_questao->FKCodStrDisciplina);
		return $_disciplina->StrDisciplina;
	}

	public function getPKCodStrDisciplina(){
		$_questao = questaoRecord::finder()->findbyPk($_SESSION['idQuestao']);
		return $_questao->FKCodStrDisciplina;
	}

	public function getStrTema(){
		$_questao = questaoRecord::finder()->findbyPk($_SESSION['idQuestao']);
		$_tema  = temaRecord::finder()->findbyPk($_questao->FKCodLongTema);
		return $_tema->StrTema;
	}

	public function getPKTema(){
		$_questao = questaoRecord::finder()->findbyPk($_SESSION['idQuestao']);
		return $_questao->FKCodLongTema;
	}
	
	public function getStrDificuldade(){
	
		
		$_questao = questaoRecord::finder()->findbyPk($_SESSION['idQuestao']);
		if ($_questao->IntDificuldade == 1){
			return "Facil";
		}else if($_questao->IntDificuldade == 2){
			return "Media";
		}else{
			return "Difícil";
		}
		
	}
	
	public function getValueDificuldade(){
		$_questao = questaoRecord::finder()->findbyPk($_SESSION['idQuestao']);
		return $_questao->IntDificuldade;
	}


}
?>